package com.well.menjin.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.well.menjin.dao.IZoneDao;
import com.well.menjin.domain.Zone;
import com.well.menjin.model.Sort;

@Repository
public class ZoneDaoImpl implements IZoneDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Zone> getZones(int start,int limit,int page,Sort sort) {
		Criteria criteria =sessionFactory.getCurrentSession().createCriteria(Zone.class).
				setFirstResult(start).setMaxResults(limit);
		if(sort!=null){
			if("ASC".equals(sort.getDirection())){
				criteria.addOrder(Order.asc(sort.getProperty()));
			}else{
				criteria.addOrder(Order.desc(sort.getProperty()));
			}
		}
				
		return criteria.list();
	}

	@Override
	public boolean isExist(String zone_no) {
		Zone zone = (Zone)sessionFactory.getCurrentSession().
		createCriteria(Zone.class).add(Restrictions.eq("zone_no", zone_no)).uniqueResult();
		if(null != zone){
			return false;
		}else {
			return true;
		}
		
	}

	@Override
	public void addZone(Zone zone) {
		sessionFactory.getCurrentSession().save(zone);
	}

	@Override
	public void updateZone(Zone zone) {
		sessionFactory.getCurrentSession().saveOrUpdate(zone);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Zone> getZonesOpts() {
		return sessionFactory.getCurrentSession().createCriteria(Zone.class).list();
	}

}
